package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.NewFileHistoryMapper;
import com.zhijian.medical.entity.po.NewFileHistory;
import com.zhijian.medical.entity.vo.request.NewFileHistorySearchVo;
import com.zhijian.medical.entity.vo.response.NewFileHistoryVo;
import com.zhijian.medical.service.NewFileHistoryService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 新文件管理修改历史记录业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-07-03 16:53:56
 */
@Slf4j
@Service
public class NewFileHistoryServiceImpl implements NewFileHistoryService {

    @Autowired
    private NewFileHistoryMapper mapper;

    @Override
    public NewFileHistory selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public NewFileHistoryVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(NewFileHistory newFileHistory) {
        Date now = new Date();
        newFileHistory.setCreateTime(now);
        newFileHistory.setUpdateTime(now);
        return mapper.insert(newFileHistory) > 0 ? newFileHistory.getId() : 0L;
    }

    @Override
    public boolean update(NewFileHistory newFileHistory) {
        Date now = new Date();
        newFileHistory.setUpdateTime(now);
        return mapper.updateByPrimaryKey(newFileHistory) != 0;
    }

    @Override
    public Integer count(NewFileHistorySearchVo newFileHistorySearchVo) {
        return mapper.count(newFileHistorySearchVo);
    }

    @Override
    public List<NewFileHistoryVo> selectList(NewFileHistorySearchVo newFileHistorySearchVo) {
        return mapper.selectList(newFileHistorySearchVo);
    }

    @Override
    public Page<NewFileHistoryVo> selectPage(NewFileHistorySearchVo newFileHistorySearchVo) {
        //先直接查总数
        int count = count(newFileHistorySearchVo);
        //分页
        Page<NewFileHistoryVo> page = new Page<>(true, count, newFileHistorySearchVo.getPageNum(), newFileHistorySearchVo.getPageSize());
        newFileHistorySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<NewFileHistoryVo> responseVos = this.selectList(newFileHistorySearchVo);
        page.setData(responseVos);
        return page;
    }

}